package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/5/8 17:17
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.server.po.Album;
import net.chenyan.server.po.User;
import net.chenyan.server.po.UserAlbum;
import net.chenyan.server.service.AlbumService;
import net.chenyan.server.service.UserAlbumService;
import net.chenyan.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.awt.print.Pageable;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/8 17:17
 */
@Slf4j
@Api(tags = {"用户专辑接口"})
@RestController
@RequestMapping(value = "/admapi/userAlbum")
public class UserAlbumController {

    @Autowired
    private UserAlbumService userAlbumService;
    @Autowired
    private UserService userService;
    @Autowired
    private AlbumService albumService;

    @ApiOperation(value = "用户点赞/取消点赞专辑")
    @PostMapping(value = "/addLikedNum")
    public R updateIsLike(@RequestBody int userId,int albumId) {
        User user=userService.getById(userId);
        Album album=albumService.getById(albumId);
        if(user==null || album==null){
            return R.error("用户id或者专辑id不存在！");
        }
        int likeNum=album.getLikedNum();
        QueryWrapper<UserAlbum> userAlbumQueryWrapper=new QueryWrapper<>();
        userAlbumQueryWrapper.eq("user_id",userId);
        userAlbumQueryWrapper.eq("album_id",albumId);
        UserAlbum userAlbum=userAlbumService.getOne(userAlbumQueryWrapper);
        if(userAlbum==null){
            UserAlbum userAlbum1=new UserAlbum();
            userAlbum1.setAlbumId(albumId);
            userAlbum1.setUserId(userId);
            userAlbum1.setIsLike(1);
            userAlbumService.save(userAlbum1);
            album.setLikedNum(likeNum+1);
            albumService.updateById(album);
            return R.ok("点赞成功");
        }else{
            if(userAlbum.getIsLike()==1){
                userAlbum.setIsLike(0);
                userAlbumService.updateById(userAlbum);
                album.setLikedNum(likeNum-1);
                albumService.updateById(album);
                return R.ok("取消点赞成功");
            }else{
                userAlbum.setIsLike(1);
                userAlbumService.updateById(userAlbum);
                album.setLikedNum(likeNum+1);
                albumService.updateById(album);
                return R.ok("点赞成功");
            }

        }

    }

    @ApiOperation(value = "用户收藏/取消收藏专辑")
    @PostMapping(value = "/addCollect")
    public R updateIsCollect(@RequestBody int userId,int albumId) {
        User user=userService.getById(userId);
        Album album=albumService.getById(albumId);
        if(user==null || album==null){
            return R.error("用户id或者专辑id不存在！");
        }
        int collectNum=album.getCollectedNum();
        QueryWrapper<UserAlbum> userAlbumQueryWrapper=new QueryWrapper<>();
        userAlbumQueryWrapper.eq("user_id",userId);
        userAlbumQueryWrapper.eq("album_id",albumId);
        UserAlbum userAlbum=userAlbumService.getOne(userAlbumQueryWrapper);
        if(userAlbum==null){
            UserAlbum userAlbum1=new UserAlbum();
            userAlbum1.setAlbumId(albumId);
            userAlbum1.setUserId(userId);
            userAlbum1.setIsCollect(1);
            userAlbumService.save(userAlbum1);
            album.setCollectedNum(collectNum+1);
            albumService.updateById(album);
            return R.ok("收藏成功");
        }else{
            if(userAlbum.getIsCollect()==1){
                userAlbum.setIsCollect(0);
                userAlbumService.updateById(userAlbum);
                album.setCollectedNum(collectNum-1);
                albumService.updateById(album);
                return R.ok("取消收藏成功");
            }else{
                userAlbum.setIsCollect(1);
                userAlbumService.updateById(userAlbum);
                album.setCollectedNum(collectNum+1);
                albumService.updateById(album);
                return R.ok("收藏成功");
            }

        }

    }

    @ApiOperation(value = "用户评论专辑")
    @PostMapping(value = "/addComment")
    public R addComment(@RequestBody int userId,int albumId,String comment) {
        User user=userService.getById(userId);
        Album album=albumService.getById(albumId);
        if(user==null || album==null){
            return R.error("用户id或者专辑id不存在！");
        }
        QueryWrapper<UserAlbum> userAlbumQueryWrapper=new QueryWrapper<>();
        userAlbumQueryWrapper.eq("user_id",userId);
        userAlbumQueryWrapper.eq("album_id",albumId);
        UserAlbum userAlbum=userAlbumService.getOne(userAlbumQueryWrapper);
        if(userAlbum==null){
            UserAlbum userAlbum1=new UserAlbum();
            userAlbum1.setAlbumId(albumId);
            userAlbum1.setUserId(userId);
            userAlbum1.setComment(comment);
            userAlbumService.save(userAlbum1);
            return R.ok("评论成功");
        }else{
            userAlbum.setComment(comment);
            boolean temp = userAlbumService.updateById(userAlbum);
            return temp ? R.ok("修改成功") : R.error("修改失败");
        }
    }
    @ApiOperation(value = "获取用户收藏的专辑")
    @GetMapping(value = "/getAlbumByUserId")
    public R getAlbumByUserId(int userId,@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(albumService.findAllAlbumByUserId(PageRequest.of(page,size),userId));
    }
}
